package pt.utl.ist.calcNotas;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;


public class MenuFantastico extends Activity {

    ArrayList<Cadeira> cadeiras;
    ArrayAdapter<Cadeira> adapter;
    ListView listView;
    final Context context = this;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_fantastico_layout);
        Button btn = (Button)findViewById(R.id.addButton);
        listView = (ListView)findViewById(R.id.cadeirasListView);
        cadeiras = new ArrayList<Cadeira>();

        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showAddDialog();
            }
        });
    }

    private void showAddDialog(){
        AlertDialog.Builder adb = new AlertDialog.Builder(context);

        adb.setTitle("Adicionar Disciplina.");

        final EditText txt = new EditText(context);
        adb.setMessage("Por favor, insira o nome da disciplina.")
                .setView(txt)
                .setPositiveButton("Adicionar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        String  str = txt.getText().toString().trim();
                        cadeiras.add(new Cadeira(str));
                        adapter.notifyDataSetChanged();
                        Toast.makeText(getBaseContext(), "A disciplina "+str+" foi adicionada com sucesso!",
                                Toast.LENGTH_LONG).show();
                        SaveData();
                    }
                })
                .setNegativeButton("Cancelar",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        Toast.makeText(getBaseContext(), R.string.inserir,
                                Toast.LENGTH_LONG).show();
                         }
                });
        AlertDialog dialog = adb.create();
        dialog.show();
    }

    protected void SaveData(){
        try
        {
            String path = this.getApplicationContext().getFilesDir() + "/storage/";
            File file = new File(path);
            file.mkdirs();
            path+="cadeiras.sav";
            ObjectOutputStream oos = new ObjectOutputStream(
                    new FileOutputStream(path, false));
            oos.writeObject(cadeiras);
            oos.flush();
            oos.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    @Override
    protected void onPause(){
        super.onPause();
        SaveData();
    }

    @Override
    protected void onResume(){
        super.onResume();
        try
        {
            String path = this.getApplicationContext().getFilesDir() + "/storage/cadeiras.sav";
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
            cadeiras = (ArrayList<Cadeira>)ois.readObject();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        adapter = new FantasticAdaptertheSecond(this,context, cadeiras);
        listView.setAdapter(adapter);

    }
}
